package com.example.ajouconqueror;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.classes.GameRoomClass;
import com.example.classes.UserApplication;

public class GameRoomAdapter extends BaseAdapter implements OnClickListener {
	// This class is adapter for gameroomactivity's list
	UserApplication UserApp;
	private Context context;
	private ArrayList<GameRoomClass> arrData;
	private LayoutInflater inflater;
	ProgressDialog progdailog;
	AlertDialog.Builder dialogBuilder;
	Context gcontext;
	int selectedroomidtp;

	public GameRoomAdapter(Context c, ArrayList<GameRoomClass> arr) {
		this.context = c;
		gcontext = c;
		this.arrData = arr;
		UserApp = (UserApplication) context.getApplicationContext();
		inflater = (LayoutInflater) c
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		try {
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int getCount() {
		return arrData.size();
	}

	public Object getItem(int position) {
		return arrData.get(position).getRoomName();
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.activity_listview_rooms,
					parent, false);
		}

		convertView.setTag(position);
		convertView.setOnClickListener(this);
		//room name setting
		TextView name = (TextView) convertView.findViewById(R.id.Roomname);
		name.setText(arrData.get(position).getRoomName());
		//count the user number in room 
		int count = 0;
		if (arrData.get(position).getTeam1_M1() != null)
			count++;
		if (arrData.get(position).getTeam1_M2() != null)
			count++;
		if (arrData.get(position).getTeam2_M1() != null)
			count++;
		if (arrData.get(position).getTeam2_M2() != null)
			count++;
		//room id setting
		int idd = arrData.get(position).getId();
		TextView roomid = (TextView) convertView.findViewById(R.id.roomid);
		roomid.setText(Integer.toString(idd));
		//user number in room setting
		TextView usercount = (TextView) convertView
				.findViewById(R.id.Usercount);
		usercount.setText(Integer.toString(count));

		return convertView;
	}

	public void Renew(ArrayList<GameRoomClass> arr) {
		//in the class you can renew the data.
		//With input ArrayList<GameRoomClass> arr, the old version is cleared and ArrayList<GameRoomClass> arr is set.
		arrData.clear();
		for (int i = 0; i < arr.size(); i++) {
			GameRoomClass data = arr.get(i);
			if (data != null)
				arrData.add(data);
		}
		this.notifyDataSetChanged();

		((GameRoomActivity) context).runOnUiThread(new Runnable() {
			public void run() {
				notifyDataSetChanged();
			}
		});
	}

	@Override
	public void onClick(View v) {
		// If you click the list item, this dialog is shown.
		// whith this dialog you can enter the room.
		int position = (Integer) v.getTag();

		GameRoomClass selectedroom = arrData.get(position);
		selectedroomidtp = selectedroom.getId();

		dialogBuilder = new AlertDialog.Builder(gcontext);

		dialogBuilder.setTitle("Room Name");

		dialogBuilder.setMessage("Do want to join?");

		dialogBuilder.setPositiveButton("Yes", yesButtonClickListener);

		dialogBuilder.setNegativeButton("No", noButtonClickListener);

		dialogBuilder.show();

	}

	private DialogInterface.OnClickListener yesButtonClickListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			// you you click the yes button in the dialog the activity is changed to teamsielction activity.
			UserApp.setRoomid(selectedroomidtp);
			Intent i = new Intent(gcontext, TeamSelection.class);
			context.startActivity(i);
		}

	};

	private DialogInterface.OnClickListener noButtonClickListener = new DialogInterface.OnClickListener() {
		//If you click the no button in dialog, it happen nothing.
		@Override
		public void onClick(DialogInterface dialog, int which) {

		}

	};

}
